<template>
    <div>
      <el-button type="primary" style="width: 100px;height: 40px;font-size: 20px;" @click="New">+ 新增</el-button>
      <el-button type="primary" style="width: 100px;height: 40px;font-size: 20px;" @click="Deletion">☝ 删除</el-button>
    </div><br>
  <div>
    <el-table :data="tableData" border style="width: 100%;" align="center"
    @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" :index="indexMethod" label="序号" width="60"/>
      <el-table-column prop="name" label="商品名称" >
        <template v-slot="scope">
          <span style="font-weight:bolder;">{{ scope.row.goodName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="traceabilityBatch" label="生产批次" />
      <el-table-column prop="traceabilityDay" label="保质期(天)" />
      <el-table-column prop="traceabilityNumber" label="生产许可证号" />
      <el-table-column prop="traceabilityLicense" label="生产企业" />
      <el-table-column prop="address" label="信息完善度" />
      <el-table-column prop="addName" label="创建人" />
      <el-table-column prop="addTime" label="创建时间" >
        <template v-slot="scope">
          <span>{{ (scope.row.addTime).substring(0,20) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="updateName" label="修改人" />
      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <el-link type="danger" style="width: 30px;" @click="Detail(scope.row)">详情</el-link>
          <el-link type="warning" style="width: 30px;" @click="Update(scope.row)">修改</el-link>
          <el-link type="primary" style="width: 30px;" @click="Del(scope.row)">删除</el-link>
      </template>
      </el-table-column>
    </el-table>
  </div>
  <el-dialog v-model="dialogForm" title="基本信息" width="566" :before-close="Close">
    <el-form :model="form" label-width="auto">
      <el-form-item label="商品名称:">
        <el-select v-model="form.goodId">
          <el-option v-for="item in Gooda" :key="item.goodId" :label="item.goodName" :value="item.goodId" />
        </el-select>
      </el-form-item>
      <el-form-item label="生产批次:">
        <el-input v-model="form.traceabilityBatch" />
      </el-form-item>
      <el-form-item label="保质期(天):">
        <el-input v-model="form.traceabilityDay" />
      </el-form-item>
      <el-form-item label="生产许可证号:">
        <el-input v-model="form.traceabilityNumber" />
      </el-form-item>
      <el-form-item label="生产企业:">
        <el-input v-model="form.traceabilityLicense" />
      </el-form-item>
      <el-form-item label="封面图:">
        <!-- <el-input v-model="form.traceabilityIamnges" /> -->
        <el-upload
          class="avatar-uploader"
          action="http://localhost:55799/api/Trace/UpdateFile"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload" >
          <img v-if="form.traceabilityIamnges" :src="'http://localhost:55799/Imge/'+form.traceabilityIamnges" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
      </el-form-item>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="form.traceabilityId==0" @click="Create(form.traceabilityId)" 
        type="primary">保存</el-button>
        <el-button v-if="form.traceabilityId!=0" @click="Create(form.traceabilityId)" 
        type="primary">修改</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import axios from 'axios';
import { onMounted, reactive, ref } from 'vue';

const form = reactive({
  traceabilityId:0,
  goodId:'',
  traceabilityBatch:'',
  traceabilityDay:'',
  traceabilityNumber:'',
  traceabilityLicense:'',
  traceabilityIamnges:''
})
const indexMethod = (index: number) => {
  return index + 1
}
//绑定商品名称
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Gooda:any=ref([])
const GetGooda=()=>{
  axios.get('Trace/GetGooda', 
  ).then((ret) => {
    console.log(ret.data)
    Gooda.value=ret.data
  })
}
const dialogForm = ref(false)
//新增
const New=()=>{
  dialogForm.value=true
  console.log(form);
}//关闭
const Close=()=>{
  location.reload();
}
//添加/修改
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Create=(row:any)=>{
  if(row==0){
    axios.post(`Trace/Add`,form).then((ret) => {
      console.log(ret.data)
      if(ret.data>0){
        ElMessage.success('添加成功')
        dialogForm.value=false
      }else{
        ElMessage.error('添加失败')
      }
    })
  }else{
    axios.post(`Trace/Update`,form).then((ret) => {
      console.log(ret.data)
      if(ret.data>0){
        ElMessage.success('修改成功')
        dialogForm.value=false
      }else{
        ElMessage.error('修改失败')
      }
    })
  }
  
}
//图片
const handleAvatarSuccess = (response:string) => {
  form.traceabilityIamnges=response
}
import { ElMessage, type UploadProps } from 'element-plus';
import { Plus } from '@element-plus/icons-vue'
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
    ElMessage.error('图片格式必须是【jpg/png】!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('图片大小不能大于 2MB!')
    return false
  }
  return true
}
onMounted(()=>{
  GetAll()
  //商品名称
  GetGooda()
})
//溯源显示列表
const tableData = ref([])
const GetAll=()=>{
  axios.get('Trace/GetAll', {
    params: {}
  }).then((ret) => {
    console.log(ret.data)
    tableData.value=ret.data
  })
}
//全选反选
const Checked=ref([])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleSelectionChange = (val:any) => {
  Checked.value = val
}

import { ElMessageBox } from 'element-plus'
import router from '@/router';
//删除
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Del=(row:any)=>{
  ElMessageBox.confirm(
    '确定删除吗？?',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      axios.delete('Trace/Delete?id='+ row.traceabilityId
      ).then((ret) => {
        console.log(ret.data)
        if(ret.data>0){
          ElMessage.success('删除成功')
          GetAll()
        }else{
          ElMessage.error('删除失败')
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'error',
        message: '取消',
      })
    })
}
//批量删除
const Deletion=()=>{

}
//修改
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Update=(row:any)=>{
  dialogForm.value=true
  axios.get('Trace/GetId', {
    params: {
      id:row.traceabilityId
    }
  }).then((ret) => {
    console.log(ret.data)
    form.traceabilityId=ret.data.traceabilityId
    form.goodId=ret.data.goodId
    form.traceabilityBatch=ret.data.traceabilityBatch
    form.traceabilityDay=ret.data.traceabilityDay
    form.traceabilityNumber=ret.data.traceabilityNumber
    form.traceabilityLicense=ret.data.traceabilityLicense
    form.traceabilityIamnges=ret.data.traceabilityIamnges
  })
}
//详情
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Detail=(row:any)=>{
  router.push({
    path:'/TraceDetail',
    query:{
      traceabilityId:row.traceabilityId
    }
  })
}


</script>

<style scoped>
.avatar-uploader .avatar {
  width: 118px;
  height: 118px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 118px;
  height: 118px;
  text-align: center;
}
</style>